import 'package:flutter/material.dart';

class InheriteWidTest extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return InheriteWidTestState();
  }
}

class InheriteWidTestState extends State<InheriteWidTest>{
  
  int count = 0;
  @override
  Widget build(BuildContext context){
    return Center(
      child: ShareDataWidget(data:count ,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.only(bottom: 20),
            child: Test(),
          ),
          RaisedButton(
            child: Text('增加'),
            onPressed: (){
              setState(() {
                ++count;
              });
            },
          )
        ],
      ),),
    );
  }

}

class Test extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return TestState();
  }
}
class TestState extends State<Test>{
  String pro;
  @override
  void didUpdateWidget(Widget oldWidget){
    super.didUpdateWidget(oldWidget);
    print('widget改变');
  }

  @override
  Widget build(BuildContext context){
    return Text(
      ShareDataWidget.of(context).data.toString()
    );
  }

  @override
  void didChangeDependencies(){
    super.didChangeDependencies();
    print('依赖改变');
  }

}

class ShareDataWidget extends InheritedWidget{
  ShareDataWidget({
    @required this.data,
    Widget child
  }):super(child:child);

  final int data;

  static ShareDataWidget of(BuildContext context){
    return context.inheritFromWidgetOfExactType(ShareDataWidget);
  }

  @override
  bool updateShouldNotify(ShareDataWidget old){
    return old.data != data;
  }
}
